package com.yiren.dbaa.module.project_edit.presenter;

import android.app.Activity;
import android.util.SparseArray;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.db.dao.cache.ProjectCache;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.line.model.LineHomeModel;
import com.yiren.dbaa.module.project_edit.contract.SiteInfoSetContract;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/11/8
 */
public class SiteInfoSetPresenter implements SiteInfoSetContract.Presenter {

    private Activity activity;
    private LineHomeContract.Model lineHomeModel;
    private UserParamConfigBean userParamConfig;

    public SiteInfoSetPresenter(Activity activity) {
        this.activity = activity;
        lineHomeModel = new LineHomeModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public List<ProjectEntity> getChoiceProjects(int type) {
        List<ProjectEntity> projects = new ArrayList<>();
        if (null != ProjectCache.subjectThreeDayProjects) {
            SparseArray<ProjectEntity> subjectThreeDayProjects = ProjectCache.subjectThreeDayProjects;
            for (int i = 0; i < subjectThreeDayProjects.size(); i++) {
                ProjectEntity projectEntity = subjectThreeDayProjects.get(subjectThreeDayProjects.keyAt(i));
                if (type == 1 && projectEntity.getName().contains("靠边")) {
                    continue;
                }
                projects.add(projectEntity);
            }
        }
        return projects;
    }

    @Override
    public void getChoiceLines(SiteInfoSetContract.View view) {
        LoadDialog.show(activity, "正在加载线路，请售后...");
        lineHomeModel.getLineFromServer(userParamConfig.getUserInfo().getpUserId().toString(), userParamConfig.getSite().getSiteCode(),false, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<LineInfoEntity> serverLines = (List<LineInfoEntity>) result;
                //获取本地列表
                List<LineInfoEntity> localLines = GradeDbController.getInstance(false).queryAll(LineInfoEntity.class);
                if (null == serverLines) {
                    view.getLineComplete(localLines);
                    return;
                }
                if (null == localLines) {
                    view.getLineComplete(serverLines);
                    return;
                }
                //数据合并去重，服务器与本地数据都存在以本地数据为准
                view.getLineComplete(lineHomeModel.lineHomedataMerge(localLines, serverLines));
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
                //获取失败加载本地
                List<LineInfoEntity> localLines = GradeDbController.getInstance(false).queryAll(LineInfoEntity.class);
                for (LineInfoEntity lineInfo : localLines) {
                    lineInfo.setDownLoad(true);
                }
                view.getLineComplete(localLines);
            }
        });
    }

    @Override
    public void filterDeleteProject(List<String> selectedProjects, List<ProjectEntity> choiceProjects) {
        for (int i = 0; i < selectedProjects.size(); i++) {
            boolean isInner = false;  //选择的项目是否在标准项目里面，如果不在，则说明该项目被删除，则删除该项目
            for (int j = 0; j < choiceProjects.size(); j++) {
                if (selectedProjects.get(i).equals(choiceProjects.get(j).getId())) {
                    isInner = true;
                    break;
                }
            }
            if (!isInner) {
                selectedProjects.remove(i);
                i--;
            }
        }
    }

    @Override
    public void filterDeleteLine(List<String> selectedLines, List<LineInfoEntity> lines) {
        for (int i = 0; i < selectedLines.size(); i++) {
            boolean isInner = false;  //选择的项目是否在标准项目里面，如果不在，则说明该线路被删除，则删除该线路
            for (int j = 0; j < lines.size(); j++) {
                if (selectedLines.get(i).equals(lines.get(j).getId())) {
                    isInner = true;
                    break;
                }
            }
            if (!isInner) {
                selectedLines.remove(i);
                i--;
            }
        }
    }
}
